//
//  ViewController.m
//  Animation
//
//  Created by unisedu on 15/7/28.
//  Copyright (c) 2015年 unisedu. All rights reserved.
//
/// pop 教程  http://www.cocoachina.com/ios/20150728/12734.html

#import "ViewController.h"
#import "POP.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *square;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self leftMoveRight];
//    [self springAnimation];
//    [self decayAnimation];
//    [self backTimes];
//    [self appleBaseAnimation];
//    [self appleSpringAnimation];
//    [self animationOfCABasicAnimation];
//    会黑屏，不清楚原因暂时
//    [self animationOfCAKeyframeAnimation];
    [self animationOfCAKeyframeAnimationPath];
}
//左右移动的基本动画
//POPBasicAnimation提供四种timingfunction
//kCAMediaTimingFunctionLinear
//kCAMediaTimingFunctionEaseIn
//kCAMediaTimingFunctionEaseOut
//kCAMediaTimingFunctionEaseInEaseOut

- (void)leftMoveRight{
    POPBasicAnimation *anBasic = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    anBasic.toValue = @(self.square.center.y+300);
    anBasic.beginTime = CACurrentMediaTime() + 1.0f;
    [self.square pop_addAnimation:anBasic forKey:@"position"];
}


//POPSpringAnimation也许是大多数人使用POP的理由，其提供一个类似弹簧一般的动画效果
//springBounciness:4.0    //[0-20] 弹力 越大则震动幅度越大
//springSpeed     :12.0   //[0-20] 速度 越大则动画结束越快
//dynamicsTension :0      //拉力  接下来这三个都跟物理力学模拟相关 数值调整起来也很费时 没事不建议使用哈
//dynamicsFriction:0      //摩擦 同上
//dynamicsMass    :0      //质量 同上
//注意:POPSpringAnimation是没有duration字段的，其动画持续时间由以上几个参数决定
- (void)springAnimation{

    POPSpringAnimation *anSpring = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    anSpring.toValue = @(self.square.center.y+300);
    anSpring.beginTime = CACurrentMediaTime() + 1.0f;
    anSpring.springBounciness = 10.0f;
    [self.square pop_addAnimation:anSpring forKey:@"position"];
}

//POPDecayAnimation提供一个过阻尼效果(其实Spring是一种欠阻尼效果)，可以实现类似UIScrollView的滑动衰减效果(是的 你可以靠它来自己实现一个UIScrollView)
//deceleration:0.998  //衰减系数(越小则衰减得越快)
//注意:这里对POPDecayAnimation设置toValue是没有意义的，会被忽略(因为目的状态是动态计算得到的)
-(void)decayAnimation{
    POPDecayAnimation *anDecay = [POPDecayAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    anDecay.velocity = @(600);
    anDecay.beginTime = CACurrentMediaTime() + 1.0f;
    [self.square pop_addAnimation:anDecay forKey:@"position"];
}



//自定义动画
//POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"prop" initializer:^(POPMutableAnimatableProperty *prop) {
//    // read value
//    prop.readBlock = ^(id obj, CGFloat values[]) {
//    };
//    // write value
//    prop.writeBlock = ^(id obj, const CGFloat values[]) {
//    };
//    // dynamics threshold
//    prop.threshold = 0.01;
//}];
//其组成就是一个readBlock一个writeBlock和一个threashold
//
//readBlock告诉POP当前的属性值
//writeBlock中修改变化后的属性值
//threashold决定了动画变化间隔的阈值，值越大writeBlock的调用次数越少

-(void)backTimes{

    POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"countdown" initializer:^(POPMutableAnimatableProperty *prop) {
        prop.writeBlock = ^(id obj, const CGFloat values[]) {
            UILabel *lable = (UILabel*)obj;
            lable.text =[NSString stringWithFormat:@"%d:%d:%d",(int)values[0]/60,(int)values[0]%60,(int)(values[0]*100)];
        };
        //        prop.threshold = 0.01f;
    }];
    POPBasicAnimation *anBasic = [POPBasicAnimation linearAnimation];   //秒表当然必须是线性的时间函数
    anBasic.property = prop;    //自定义属性
    anBasic.fromValue = @(0);   //从0开始
    anBasic.toValue = @(3*60);  //180秒
    anBasic.duration = 3*60;    //持续3分钟
    anBasic.beginTime = CACurrentMediaTime() + 1.0f;    //延迟1秒开始
    [self.label pop_addAnimation:anBasic forKey:@"countdown"];
}



//苹果线性动画
-(void)appleBaseAnimation{
//    右移动
//    [UIView animateWithDuration:1 animations:^{
//        CGFloat temp = self.view.bounds.size.width;
//        CGFloat squarex =self.square.center.x;
//        [self.square setCenter:CGPointMake(temp-squarex, self.square.center.y)];
//        
//    }];
//    左右来回动
//    [UIView animateWithDuration:5 delay:1 options:UIViewAnimationOptionAutoreverse animations:^{
//        CGFloat temp = self.view.bounds.size.width;
//        CGFloat squarex =self.square.center.x;
//        [self.square setCenter:CGPointMake(temp-squarex, self.square.center.y)];
//    } completion:nil];
////    重复一个动作
//    [UIView animateWithDuration:5 delay:1 options:UIViewAnimationOptionRepeat animations:^{
//        CGFloat temp = self.view.bounds.size.width;
//        CGFloat squarex =self.square.center.x;
//        [self.square setCenter:CGPointMake(temp-squarex, self.square.center.y)];
//    } completion:nil];
    
    [UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionCurveEaseIn animations:^{
        CGFloat temp = self.view.bounds.size.width;
        CGFloat squarex =self.square.center.x;
        [self.square setCenter:CGPointMake(temp-squarex, self.square.center.y)];
    } completion:nil];
    
}

-(void)appleSpringAnimation{
    [UIView animateWithDuration:1 delay:1 usingSpringWithDamping:0.5 initialSpringVelocity:1 options:UIViewAnimationOptionLayoutSubviews animations:^{
        CGFloat temp = self.view.bounds.size.width;
        CGFloat squarex =self.square.center.x;
        [self.square setCenter:CGPointMake(temp-squarex, self.square.center.y)];
    } completion:nil];
}

-(void)animationOfCABasicAnimation
{
    //创建一个CABasicAnimation对象
    CABasicAnimation *animation=[CABasicAnimation animation];
    //设置颜色
    animation.toValue=(id)[UIColor blueColor].CGColor;
    //动画时间
    animation.duration=1;
    //是否反转变为原来的属性值
    animation.autoreverses=YES;
    //把animation添加到图层的layer中，便可以播放动画了。forKey指定要应用此动画的属性
    [self.view.layer addAnimation:animation forKey:@"backgroundColor"];
    
}

-(void)animationOfCAKeyframeAnimation
{
    //初始化一个View，用来显示动画
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
    redView.backgroundColor=[UIColor redColor];
    
    [self.view addSubview:redView];
    
    CAKeyframeAnimation *animation=[CAKeyframeAnimation animation];
    //设置属性值
    animation.values=[NSArray arrayWithObjects:
                      (id)self.view.backgroundColor,
                      (id)[UIColor yellowColor],
                      (id)[UIColor greenColor],
                      (id)[UIColor blueColor],nil];
    animation.duration=1;
    animation.autoreverses=YES;
    //把关键帧添加到layer中
    [redView.layer addAnimation:animation forKey:@"backgroundColor"];
}
-(void)animationOfCAKeyframeAnimationPath
{
    //初始化一个View，用来显示动画
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
    redView.backgroundColor=[UIColor redColor];
    
    [self.view addSubview:redView];
    
    CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];
    CGMutablePathRef aPath=CGPathCreateMutable();
    
    CGPathMoveToPoint(aPath, nil, 20, 20);
    CGPathAddCurveToPoint(aPath, nil,
                          160, 30,
                          220, 220,
                          240, 380);
    
    ani.path=aPath;
    ani.duration=10;
    ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    ani.rotationMode=@"auto";
    [redView.layer addAnimation:ani forKey:@"position"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
